﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HostsOperate
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        HostsHelper _helper = null;
        public MainWindow()
        {
            InitializeComponent();
            txtIp.Text = IpHelper.GetLocalIp();
            try
            {
                _helper = new HostsHelper();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //绑定列表
            Rebind();
        }
        //添加域名和IP绑定
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string ip = txtIp.Text;
            string hostname = txtHostName.Text;
            if (string.IsNullOrEmpty(hostname))
            {
                txtHostName.Focus();
                ShowResult("请输入域名");
                return;
            }
            if (string.IsNullOrEmpty(ip))
            {
                txtIp.Focus();
                ShowResult("请输入IP地址");
                return;
            }


            try
            {
                //添加结果
                _helper.Add(new HostData(true, ip, hostname));
                MessageBox.Show("添加成功");
                //重新绑定
                Rebind();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                btnAdd.IsEnabled = false;
            }
        }
        //重新绑定结果
        public void Rebind()
        {
            gridOne.ItemsSource = _helper.HostDatas;
            gridOne.Items.Refresh();
        }
        //显示结果
        public void ShowResult(string str)
        {
            Result.Content = str;
        }
        //删除内容
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            MessageBoxResult result = MessageBox.Show("确定要删除吗？", "提示", MessageBoxButton.YesNo);
            if (result == MessageBoxResult.Yes)
            {
                _helper.Remove(new HostData(true, btn.Uid.ToString(), btn.ContentStringFormat.ToString()));
                Rebind();
                ShowResult("删除成功");
            }
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox check = sender as CheckBox;

            _helper.Enable(check.Uid.ToString(), check.ContentStringFormat.ToString());
            Rebind();

            string result = check.IsChecked == true ? "禁用成功" : "启用成功";
            ShowResult(result);
        }


        #region 导航菜单
        //微工具集
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.gongjuji.net/?from=hostoperate");
        }
        //关于框
        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            About _aoubt = new About();
            _aoubt.ShowDialog();
        }
        #endregion
    }
}
